package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.InvoiceDao;
import cn.gson.crm.model.dao.OutboundDao;
import cn.gson.crm.model.mapper.*;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class InvoiceService {
    @Autowired
    InvoiceMapper invoiceMapper;
    @Autowired
    ContractMapper contractMapper;
    @Autowired
    OrderSerivice orderSerivice;
    @Autowired
    DeliverRecordMapper deliverRecordMapper;
    @Autowired
    InvoiceDao invoiceDao;
    @Autowired
    DeliveryPlanMapper deliveryPlanMapper;
    @Autowired
    OutboundDao outboundDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前人发货单
    public Map<String,Object> selectfahuo(HttpSession session,Integer yid,String dept,Integer page, Integer size){
        yid=fouyuan(session);
        List<Invoice> invoices=invoiceMapper.selectfahuo(yid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Invoice> invoicesList=invoiceMapper.selectfahuo(yid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        //新增审批批人下拉框
        List<Employee> employees=contractMapper.selectsp(dept);
        List<Employee> employeeList=contractMapper.selectLB();
        Map<String,Object> map = new HashMap<>();
        //查询已审批但未发货的订单
        List<Order> orders=invoiceMapper.selectorder();
        map.put("total",obj.getTotal());
        map.put("rows",invoicesList);
        map.put("employees",employees);
        map.put("employeeList",employeeList);
        map.put("tong",invoices);
        map.put("jinli",employees);
        map.put("orderlist",orders);
        map.put("fhdanhao","FH"+newDate+result);
        map.put("chukdanhao","CK"+newDate+result);
        map.put("lb",employeeList);
        return map;
    };
    //条件查询发货单
    public Map<String,Object> selectwhere(HttpSession session,Integer yid, String where,Integer page, Integer size){
        yid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Invoice> invoicesList=invoiceMapper.selectwhere(yid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoicesList);
        return map;
    };
    //时间查询发货单
    public Map<String,Object> selecttime(HttpSession session,Integer yid,String statr,String endtime,Integer page, Integer size){
        yid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Invoice> invoicesList=invoiceMapper.selecttime(yid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoicesList);
        return map;
    };
    //根据订单id查询状态未交付的交付计划
    public List<DeliveryPlan> selectDelivery(Integer oid){
        return invoiceMapper.selectDelivery(oid);
    };
    //根据交付计划查询客户信息
    public Map<String,Object> selectcusomt(Integer cid,Integer pid){
        Map<String,Object> map = new HashMap<>();
        List<Customer> deliveryPlans=invoiceMapper.selectcusetomer(cid);
        List<Linkman> linkmanList=invoiceMapper.selectlink(cid);
        map.put("deliveryPlans",deliveryPlans);
        map.put("linkmanList",linkmanList);
        map.put("demap",deliveryPlanMapper.dedeta(pid));
        return map;
    };
    //新增
    public void save(HttpSession session,Invoice invoice){
        if(invoice.getLnId()>0){
            invoiceDao.save(invoice);
        }else{
            invoiceDao.save(invoice);
            Check check = new Check();
            User login = (User)session.getAttribute("login");
            int yname= (int) login.getEmployees().get(0).getYgId();
            check.setChecksqinName((long) yname);
            check.setCheckType("发货审批");
            check.setChecktypeid((int) invoice.getLnId());
            check.setCheckDate(invoice.getLnTime());
            check.setCheckName(invoice.getEreApprover());
            check.setCheckCondition("未审核");
        }
    }
    public List<Employee>selectem(Integer yid){
        return invoiceMapper.selectem(yid);
    };
    public void updatejiao(String pid){
      invoiceMapper.updatejiao(pid);
    };
    //发货操作
    public void fahuo(Integer oid,Integer pid,Integer lid){
        deliverRecordMapper.updatedel(pid);
        invoiceMapper.updatefah(lid);
    }
    public List<Invoice> selectfahuocustm(Integer cid){
      return   invoiceMapper.selectfahuocustm(cid);
    };
    //状态修改退换否
    public void updatefahuo(String tf, Integer lid){
        invoiceMapper.updatefahuo(tf,lid);
    };
}
